﻿using Barcode_Project.Entity;
using Barcode_Project.Network;
using DevExpress.Xpo.DB;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Barcode_Project.ProBarPrint
{
    public partial class RelativeBarCodes : DevExpress.XtraEditors.XtraForm
    {
        public string selectedBarCodes { get; set; } = "";

        public List<UsedBarCode> outputBarCodes { get; set; }

        public RelativeBarCodes(string plCode, string gxCode)
        {
            InitializeComponent();

            LoadBarCodeLibrary(plCode, gxCode);
        }
        // 根据父页面提供的信息 加载条码
        void LoadBarCodeLibrary(string plCode, string gxCode)
        {
            var data = new RemoteRequest().GetRelativeBarCodes(plCode, gxCode);

            gridControl_library.DataSource = data;
            gridView_library.PopulateColumns();
            gridView_library.OptionsView.ColumnAutoWidth = false;
            gridView_library.BestFitColumns();
            //gridControl_library.RefreshDataSource();

            var selectedData = new List<UsedBarCode>();

            gridControl_selected.DataSource = selectedData;
            gridControl_selected.RefreshDataSource();

            // 让使用米数和使用重量两列可编辑，其它列不可编辑
            EnableTwoColumnEditable();
        }

        void EnableTwoColumnEditable()
        {
            foreach (GridColumn col in gridView_selected.Columns)
            {
                if (col.FieldName == "used_meter" || col.FieldName == "used_weight")
                {
                    col.OptionsColumn.ReadOnly = false;
                }
                else
                {
                    col.OptionsColumn.ReadOnly = true;
                }
            }
        }

        public void RenderSelectedBarCodes()
        {
            //var lib_list = gridControl_library.DataSource as List<RelativeBarCode>;

            //var selectedList = lib_list.Where(l => selectedBarCodes.Contains(l.barCode)).ToList();


            //var usedBarCodes = new List<UsedBarCode>();
            //foreach (var item in selectedList)
            //{
            //    var used = new UsedBarCode()
            //    {
            //        barCode = item.barCode,
            //        proCode = item.proCode,
            //        proName = item.proName,
            //        createtime = item.createtime,
            //        gxName = item.gxName,
            //        meter = item.meter,
            //        number = item.number,
            //        spec = item.spec,
            //        weight = item.weight,
            //    };

            //    usedBarCodes.Add(used);
            //}

            gridControl_selected.DataSource = outputBarCodes;
            gridView_selected.PopulateColumns();
            gridView_selected.OptionsView.ColumnAutoWidth = false;
            gridView_selected.BestFitColumns();
            //gridControl_selected.RefreshDataSource();

            EnableTwoColumnEditable();

        }

        private void button_select_Click(object sender, EventArgs e)
        {
            var lib_selected_indexes = gridView_library.GetSelectedRows();

            var lib_list = gridControl_library.DataSource as List<RelativeBarCode>;

            var selected_list = gridControl_selected.DataSource as List<UsedBarCode>;

            if (selected_list == null)
            {
                selected_list = new List<UsedBarCode>();
            }

            foreach (var index in lib_selected_indexes)
            {
                var barCode = lib_list[index].barCode;

                var exist = selected_list.Exists(s => s.barCode == barCode);

                if (!exist)
                {
                    var item = lib_list[index];

                    selected_list.Add(new UsedBarCode()
                    {
                        barCode = item.barCode,
                        proCode = item.proCode,
                        proName = item.proName,
                        createtime = item.createtime,
                        gxName = item.gxName,
                        proMeter = item.proMeter,
                        number = item.number,
                        spec = item.spec,
                        proWeight = item.proWeight,
                        cu_iUnitCost = item.cu_iUnitCost,
                        cu_batch = item.cu_batch,
                    });
                }
            }

            gridControl_selected.DataSource = selected_list;

            gridView_selected.PopulateColumns();
            gridView_selected.OptionsView.ColumnAutoWidth = false;
            gridView_selected.BestFitColumns();
            gridControl_selected.RefreshDataSource();

            EnableTwoColumnEditable();
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            var selected_selected_indexes = gridView_selected.GetSelectedRows();

            var selected_list = gridControl_selected.DataSource as List<UsedBarCode>;

            foreach (var index in selected_selected_indexes.OrderByDescending(i => i))
            {
                selected_list.RemoveAt(index);
            }

            gridControl_selected.DataSource = selected_list;
            gridControl_selected.RefreshDataSource();

            gridView_selected.ClearSelection();
        }

        private void button_confirm_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            gridControl_library.Focus();

            var selected_list = gridControl_selected.DataSource as List<UsedBarCode>;
            selectedBarCodes = string.Join(",", selected_list.Select(s => s.barCode));

            outputBarCodes = gridControl_selected.DataSource as List<UsedBarCode>;

            this.Close();
        }

        private void btn_Close_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void RelativeBarCodes_Load(object sender, EventArgs e)
        {

        }
    }
}